//
//  ReadDetailViewController.m
//  News
//
//  Created by lanou3g on 15/11/12.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "ReadDetailViewController.h"
#import "ReadDetailHelper.h"
#import "ReadModel.h"
#import "CommonTool.h"

@interface ReadDetailViewController ()

@property (nonatomic, copy) NSString *html;

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation ReadDetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.hidesBottomBarWhenPushed = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.hidesBottomBarWhenPushed = YES;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    
    self.navigationItem.title = @"新闻详情";
    
    UIButton *favorateBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 50, 20, 30, 30)];
    [favorateBtn setImage:[UIImage imageNamed:@"fortune_nor_focus"] forState:UIControlStateNormal];
    [favorateBtn setImage:[UIImage imageNamed:@"fortune_focus"] forState:UIControlStateSelected];
    [favorateBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:favorateBtn];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setModel:(ReadModel *)model
{
    _model = model;
    
    
    [ReadDetailHelper sharedReadDetailHelper].block = ^(NSString *html) {
        self.html = html;
    };
    
    [[ReadDetailHelper sharedReadDetailHelper] getDataFromWebWithString:model];
    
    self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.webView];
    [self.webView loadHTMLString:self.html baseURL:nil];
}

- (void)btnClicked:(UIButton *)btn
{
    btn.selected = !btn.selected;
    
    BmobObject  *news = [BmobObject objectWithClassName:@"News"];
    [news setObject:_model.title forKey:@"title"];
    [news setObject:_model.source forKey:@"source"];
    [news setObject:_model.img forKey:@"pic_url"];
    [news setObject:_html forKey:@"html_string"];
    
    //异步保存到服务器
    [news saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
         
            [CommonTool showSuccess:@"收藏成功"];
            
        }
        
    }];
}


@end
